package org.edu.modules.act.controller;


import org.apache.shiro.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.edu.common.api.vo.Result;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.act.common.result.ActionResult;
import org.edu.modules.act.common.result.ListResult;
import org.edu.modules.act.common.result.ResultBuilder;
import org.edu.modules.act.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;

/**
 * 任务信息的控制层
 *
 * @版权所有 福州土著人信息科技有限公司
 */
@Api(tags="任务模块")
@RestController
@RequestMapping("/activiti/task")
public class TaskController {

	@Autowired
	private TaskService taskService;

	/**
	 * 查询任务分页
	 *
	 * @param current 当前页数
	 * @param pageSize    每页记录数
	 * @param taskName    任务名称
	 * @return
	 */
	@GetMapping(path = "/queryTask")
	@ApiOperation(value="查询任务分页", notes="任务模块-查询任务分页")
	public Result<?> queryTask(Integer current,Integer pageSize,String taskName) {
		Map<String, Object> data = taskService.queryTask(current, pageSize, taskName);
		return Result.OK(data);
	}

	/**
	 * 签收任务
	 *
	 * @param taskId 任务ID
	 * @return
	 */
	@ApiOperation(value="签收任务", notes="任务模块-签收任务")
	@PostMapping(path = "/claimTask")
	public ActionResult claimTask(String taskId) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		taskService.claimTask(taskId, user.getId());
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 完结任务，任务进入下一节点
	 *
	 * @param taskId            任务ID
	 * @param processInstanceId 流程实例ID
	 * @param candidate         下一节点候选者
	 * @param comment           批注
	 * @return
	 */
	@ApiOperation(value="完结任务，任务进入下一节点", notes="任务模块-完结任务，任务进入下一节点")
	@PostMapping(path = "/completeTask")
	public ActionResult completeTask(String taskId,String processInstanceId,String candidate,String comment) {
		System.out.println(taskId+"***"+processInstanceId+"***"+candidate+"***"+comment);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		taskService.completeTask(taskId, processInstanceId, candidate, comment, user.getId());
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 委派任务
	 *
	 * @param taskId            任务ID
	 * @param processInstanceId 流程实例ID
	 * @param assignee          签收者
	 * @param comment           批注
	 * @return
	 */
	@ApiOperation(value="委派任务", notes="任务模块-委派任务")
	@PostMapping(path = "/delegateTask")
	public ActionResult delegateTask(String taskId,String processInstanceId,String assignee,String comment) {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		taskService.delegateTask(taskId, processInstanceId, comment, authentication.getName(), assignee);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 被委派人完结任务并返回
	 *
	 * @param taskId            任务ID
	 * @param processInstanceId 流程实例ID
	 * @param comment           批注
	 * @return
	 */
	@ApiOperation(value="被委派人完结任务并返回", notes="任务模块-被委派人完结任务并返回")
	@PostMapping(path = "/resolveTask")
	public ActionResult resolveTask(String taskId,String processInstanceId,String comment) {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		taskService.resolveTask(taskId, processInstanceId, comment, authentication.getName());
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 回退任务
	 *
	 * @param taskId            任务ID
	 * @param processInstanceId 流程实例ID
	 * @param comment           批注
	 * @return
	 */
	@ApiOperation(value="回退任务", notes="任务模块-回退任务")
	@PostMapping(path = "/regressTask")
	public ActionResult regressTask(String taskId,String processInstanceId,String comment) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		taskService.regressTask(taskId, processInstanceId, comment, user.getId());
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 驳回任务
	 *
	 * @param taskId            任务ID
	 * @param processInstanceId 流程实例ID
	 * @param comment           批注
	 * @return
	 */
	@ApiOperation(value="驳回任务", notes="任务模块-驳回任务")
	@PostMapping(path = "/rejectTask")
	public ActionResult rejectTask(String taskId,String processInstanceId,String comment) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		taskService.rejectTask(taskId, processInstanceId, comment, user.getId());
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 终止任务
	 *
	 * @param taskId            任务ID
	 * @param processInstanceId 流程实例ID
	 * @param comment           批注
	 * @return
	 */
	@ApiOperation(value="终止任务", notes="任务模块-终止任务")
	@PostMapping(path = "/terminateTask")
	public ActionResult terminateTask(String taskId,String processInstanceId,String comment) {
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		taskService.terminateTask(taskId, processInstanceId, comment, user.getId());
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 查询个人任务分页
	 *
	 * @param currentPage 当前页数
	 * @param pageSize    每页记录数
	 * @param taskStatus  任务状态
	 * @return
	 */
	@ApiOperation(value="查询个人任务分页", notes="任务模块-查询个人任务分页")
	@GetMapping(path = "/queryPersonalTask")
	public ListResult<Object> queryPersonalTask(Integer currentPage,Integer pageSize,String taskName,String taskStatus) {
		System.out.println(taskStatus);
		System.out.println(currentPage);
		System.out.println(pageSize);
		System.out.println(taskName);
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		Map<String, Object> data = taskService.queryPersonalTask(currentPage, pageSize, user.getId(), taskName, taskStatus);
		return ResultBuilder.buildListSuccess(data);
	}

	/**
	 * 查询代理人的下拉框数据
	 *
	 * @return
	 */
	@ApiOperation(value="查询代理人的下拉框数据", notes="任务模块-查询代理人的下拉框数据")
	@GetMapping(path = "/queryUsername")
	public ListResult<Object> queryUsername() {
		List<IdentityHashMap<String, Object>> data = new ArrayList<IdentityHashMap<String, Object>>();
	/* *
		List<LinkedHashMap<String, Object>> sysOrgList = sysOrgServiceClient.querySysOrgList("null");
		for (int i = 0; i < sysOrgList.size(); i++) {
			IdentityHashMap<String, Object> optGroupMap = new IdentityHashMap<>();
			for (Entry<String, Object> sysOrgEntry : sysOrgList.get(i).entrySet()) {
				String sysOrgKey = sysOrgEntry.getKey();
				Object sysOrgValue = sysOrgEntry.getValue();
				if (sysOrgKey.equals("orgName")) {
					optGroupMap.put("value", sysOrgValue);
				}
				if (sysOrgKey.equals("id")) {
					List<LinkedHashMap<String, Object>> sysUserList = sysUserServiceClient.querySysUserList(Long.valueOf(sysOrgValue.toString()));
					List<IdentityHashMap<String, Object>> childrenList = new ArrayList<IdentityHashMap<String, Object>>();
					for (int j = 0; j < sysUserList.size(); j++) {
						IdentityHashMap<String, Object> optionMap = new IdentityHashMap<>();
						StringBuilder stringBuilder = new StringBuilder();
						for (Entry<String, Object> sysUserEntry : sysUserList.get(j).entrySet()) {
							String sysUserKey = sysUserEntry.getKey();
							Object sysUserValue = sysUserEntry.getValue();
							if (sysUserKey.equals("username")) {
								stringBuilder.append(sysUserValue);
								optionMap.put("key", sysUserValue);
							}
							if (sysUserKey.equals("nickname")) {
								stringBuilder.append("(" + sysUserValue + ")");
							}
						}
						optionMap.put("value", stringBuilder.toString());
						childrenList.add(optionMap);
					}
					optGroupMap.put("children", childrenList);
				}
			}
			if (!optGroupMap.get("children").toString().equals("[]")) {
				data.add(optGroupMap);
			}
		}
*/
		return ResultBuilder.buildListSuccess(data);
	}

	/**
	 * 查询任务批注
	 *
	 * @param processInstanceId 流程实例ID
	 * @return
	 */
	@ApiOperation(value="查询任务批注", notes="任务模块-查询任务批注")
	@GetMapping(path = "/queryTaskComment")
	public ListResult<Object> queryTaskComment(String processInstanceId) {
		Map<String, Object> data = taskService.queryTaskComment(processInstanceId);
		return ResultBuilder.buildListSuccess(data);
	}

}
